﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Step4.Unit7.Model.Entities;
using Step4.Unit7.Service;
using Step4.Unit7.Service.Dto;
using Step4.Unit7.Service.Dto.Condition;
using Step4.Unit7.Service.utils;
using X.PagedList;

namespace Step4.Unit7.EFCore.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAccountService _accountService;
        private readonly ILogger<AccountController> _logger;
        public AccountController(IAccountService accountService, ILogger<AccountController> logger)
        {
            _accountService = accountService;
            _logger = logger;
        }

        public IActionResult Search(AccountRequest? request)
        {
            var list = _accountService.Search(request);
            ViewBag.StateList = EnumHelper.ToDescriptionDictionary<AccountStateEnum>() // 转换为枚举项描述的字典集合
                .Select(p => new SelectListItem
                {
                    Text = p.Value,
                    Value = p.Key.ToString()
                }).ToList() ;
            ViewBag.StateValue = (int)request.AccountState;
            var pageModel= new StaticPagedList<AccountViewModel>(list, request.PageIndex, request.PageSize, request.Total);
            return View(pageModel); // 把视图模型传过去
        }


        /// <summary>
        /// 添加页面视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            ViewBag.StateList = EnumHelper.ToDescriptionDictionary<AccountStateEnum>()
                .Select(p => new SelectListItem
                {
                    Value =p.Key.ToString(),
                    Text =p.Value
                });
            ViewBag.DebtorList = new SelectList(_accountService.GetDebtors(), "Id", "NickName");
            return View();
        }

        /// <summary>
        /// 添加提交
        /// </summary>
        [HttpPost]
        public IActionResult SubmitAdd(AccountBo bo)
        {
            if (ModelState.IsValid)
            {
                _accountService.Add(bo);
                return RedirectToAction("Search","Account");
            }
            return View("Add",bo);
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        public IActionResult Edit(long id)
        {
            AccountUpdateBo account = _accountService.GetModel(id);
            ViewBag.StateList = EnumHelper.ToDescriptionDictionary<AccountStateEnum>()
                .Select(e => new SelectListItem
                {
                    Value = e.Key.ToString(),
                    Text = e.Value
                });
            ViewBag.DebtorList = new SelectList(_accountService.GetDebtors(),"Id","NickName");
            return View(account);
        }

        /// <summary>
        /// 修改提交
        /// </summary>
        [HttpPost]
        public IActionResult SubmitUpdate(AccountUpdateBo bo)  
        {
            if (ModelState.IsValid)
            {
                _accountService.Update(bo);
                return RedirectToAction("Search", "Account");
            }

            return View("Update", bo);
        }

        public IActionResult Delete(long id)
        {
            _accountService.Delete(id);
            return RedirectToAction("Search", "Account");
        }
    }
}
